package pt.compflow.client.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.restlet.representation.Representation;
import org.restlet.resource.Get;

import pt.compflow.Context;
import pt.compflow.client.BaseResource;
import pt.compflow.flowontology.model.FlowOntologyModel;
import pt.compflow.flowontology.model.PersonGroup;
import pt.compflow.storage.TripleStoreConnection;

public class RegisterUIResource extends UIBaseResource {

//	private static final Logger LOG = LoggerFactory.getLogger(PersonResource.class);
	
	@Get
	public Representation get() {
		Context context = getCompFlowContext();
		TripleStoreConnection store = context.getStoreConnection();
		store.openStore();
		
		FlowOntologyModel model = store.connectModel();
				
		Map<String, Object> argumentMap = new HashMap<String, Object>();
		argumentMap.put("groups", getGroups(context, model));
		
		store.closeStore();
		
		return getTemplateRepresentation("RegisterTemplate.ftl", argumentMap);
	}
	
	private List<Map<String, Object>> getGroups(Context context, FlowOntologyModel model) {
		
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		
		Iterator<PersonGroup> it = model.listPersonGroups();
		while(it.hasNext()) {
			PersonGroup g = it.next();
			
			if(g.getURI().compareTo(BaseResource.ADMIN_LOCAL_GROUP_URI) != 0) {
				Map<String, Object> gMap = new HashMap<String, Object>();
				gMap.put("uri", g.getURI());
				gMap.put("shortUri", g.getLocalName());
				gMap.put("label", g.getLabel(null));
				gMap.put("description", g.getDescription(null));
				result.add(gMap);
			}
			
		}
		
		return result;
		
	}
	
	
}
